package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.*;
import com.cqie.tes.domain.vo.SysMenuFuncVo;
import com.cqie.tes.domain.vo.SysRoleMenuVo;
import com.cqie.tes.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqie.tes.service.RoleMenuService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RoleFuncMapper roleFuncMapper;
    @Autowired
    private SysFuncMapper sysFuncMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;


    /**
     * 根据角色ID查询权限树形结构
     */
    @Override
    public R getRolePermissionByRoleId(Integer id) {
        // 构建查询条件
        List<SysMenuFuncVo> sysMenuFunVos = new ArrayList<>();
        // 根据角色ID查询授权的菜单ID
        QueryWrapper<RoleMenu> mqw = new QueryWrapper<>();
        mqw.eq("roleId", id);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(mqw);
        List<Long> menuIds = new ArrayList<>();
        roleMenus.forEach(roleMenu -> {
            menuIds.add(roleMenu.getMenuId());
        });
        // 角色ID查询授权的操作ID
        QueryWrapper<RoleFunc> ffqw = new QueryWrapper<>();
        ffqw.eq("roleId", id);
        List<RoleFunc> roleFuncs = roleFuncMapper.selectList(ffqw);
        List<Long> funcIds = new ArrayList<>();
        roleFuncs.forEach(roleFunc -> {
            funcIds.add(roleFunc.getFuncId());
        });

//        System.out.println(Arrays.toString(funcIds.toArray()));
        // 父级菜单列表
        QueryWrapper<SysMenu> fqw = new QueryWrapper<>();
        fqw.eq("mark", 1);
        fqw.eq("parentId", 0);
        fqw.orderByAsc("sortCode");
        List<SysMenu> sysFMenuList = sysMenuMapper.selectList(fqw);
        sysFMenuList.forEach(sysFMenu -> {
            SysMenuFuncVo sysFMenuFuncVo = new SysMenuFuncVo();
            sysFMenuFuncVo.setTitle(sysFMenu.getName());
            sysFMenuFuncVo.setField("menuId_" + sysFMenu.getId());
            sysFMenuFuncVo.setId("menuId_" + sysFMenu.getId());
            // 根据父级菜单查询子级菜单
            List<SysMenuFuncVo> sysCMenuFunVos = new ArrayList<>();
            QueryWrapper<SysMenu> cqw = new QueryWrapper<>();
            cqw.eq("mark", 1);
            cqw.eq("parentId", sysFMenu.getId());
            cqw.orderByAsc("sortCode");
            List<SysMenu> sysCMenuList = sysMenuMapper.selectList(cqw);
            sysCMenuList.forEach(sysCMenu -> {
                SysMenuFuncVo sysCMenuFuncVo = new SysMenuFuncVo();
                sysCMenuFuncVo.setTitle(sysCMenu.getName());
                sysCMenuFuncVo.setField("menuId_" + sysCMenu.getId());
                sysCMenuFuncVo.setId("menuId_" + sysCMenu.getId());
                // 根据子级菜单查询操作
                QueryWrapper<SysFunc> funcqw = new QueryWrapper<>();
                funcqw.eq("menuId", sysCMenu.getId());
                funcqw.orderByAsc("sortCode");
                List<SysFunc> sysFuncs = sysFuncMapper.selectList(funcqw);
                List<SysMenuFuncVo> sysFuncMenuFunVos = new ArrayList<>();
                sysFuncs.forEach(sysFunc -> {
                    SysMenuFuncVo sysFuncMenuFuncVo = new SysMenuFuncVo();
                    sysFuncMenuFuncVo.setTitle(sysFunc.getName());
                    sysFuncMenuFuncVo.setField("funcId_" + sysFunc.getId());
                    sysFuncMenuFuncVo.setId("funcId_" + sysFunc.getId());
                    if (funcIds.indexOf(sysFunc.getId()) != -1) {
                        sysFuncMenuFuncVo.setChecked(true);
                    }
                    // 操作
                    sysFuncMenuFunVos.add(sysFuncMenuFuncVo);
                });
                sysCMenuFuncVo.setChildren(sysFuncMenuFunVos);
                sysCMenuFunVos.add(sysCMenuFuncVo);

            });
            sysFMenuFuncVo.setChildren(sysCMenuFunVos);
            sysMenuFunVos.add(sysFMenuFuncVo);
        });

        //

        return R.ok().message("查询成功").data("tree", sysMenuFunVos);
    }


    @Transactional
    public R setRolePermission(Long id, Map<String, String> funcMap) {
//        if ()、
        System.out.println();
        if (sysRoleMapper.selectById(id) == null){
            return R.error().message("角色不存在");
        }
        // 删除角色菜单
        QueryWrapper<RoleMenu> rmqw = new QueryWrapper<>();
        rmqw.eq("roleId", id);
        roleMenuMapper.delete(rmqw);
        // 删除角色操作
        QueryWrapper<RoleFunc> rfqw = new QueryWrapper<>();
        rfqw.eq("roleId", id);
        roleFuncMapper.delete(rfqw);
        try {
            funcMap.keySet().forEach(item -> {
                String value = funcMap.get(item);

                String[] args = value.split("_");
                if (args[0].equals("funcId")) {
                    RoleFunc roleFunc = new RoleFunc();
                    roleFunc.setFuncId(Long.parseLong(args[1]));
                    roleFunc.setRoleId(id);
                    roleFuncMapper.insert(roleFunc);
                } else if (args[0].equals("menuId")) {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setMenuId(Long.parseLong(args[1]));
                    roleMenu.setRoleId(id);
                    roleMenuMapper.insert(roleMenu);
                }

            });
        } catch (Exception e) {
            return R.error().message("更新权限失败");
        }
        return R.ok().message("更新权限成功");
    }
}
